package com.easyban.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyban.dto.OrdersDto;
import com.easyban.entity.Orders;
import com.easyban.entity.OrdersDetail;
import com.easyban.mapper.OrdersDetailMapper;
import com.easyban.mapper.OrdersMapper;
import com.easyban.service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;

    @Transactional
    @Override
    public void addOrderDto(OrdersDto ordersDto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersDto, orders);
        super.save(orders);

        List<OrdersDetail> list = ordersDto.getOrdersDetailList();

        for (OrdersDetail detail : list) {
            detail.setOrdersId(orders.getId());
            ordersDetailMapper.insert(detail);
        }

    }
}
